<?php
namespace app\controller;

use app\service\Config;
use app\service\Controller;

class LoginController extends Controller
{

    public $layout = false;

    /**
     * index
     * @return mixed
     */
    public function index()
    {
        $tokenConfig = Config::getInstance()->get('token');
        if($this->request->isPost()){
            $username = $this->request->post('username');
            $password = $this->request->post('password');
            if($username == $tokenConfig['username'] && $password == $tokenConfig['password']){
                $this->response->cookie('token',get_token($this->request->header('user-agent')),time()+$tokenConfig['expire']??7200);
                return $this->success();
            }else{
                return $this->error('登录失败.');
            }
        }else{
            $token = $this->request->cookie('token');
            if($token == get_token($this->request->header('user-agent'))){
                return $this->redirect('/');
            }
            return $this->render('index',['time'=>time()]);
        }
    }

}